package com.shuaiche.yeguan.model;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.alibaba.fastjson.JSONObject;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.util.List;

/**
 * 类描述：分享内容
 *
 * @auther: baoyinbo
 * @date: 2018/4/12 下午4:03
 */

public class ShareObj implements Serializable {
    private String shareTitle;
    private String shareContent;
    private String shareUrl;
    private String shareImage;

    private byte[] bytePic;
    private List<String> picUrls;
    private JSONObject paramsJson;      //后缀参数


    private String bizType;         //分享类型
    private Long dataMerchantId;    //车辆所属商户id
    private String bizData;         //分享车辆id 或 条件


    /**
     * 多图分享文案
     */
    private String multiContent;

//    private Bitmap imageShare;

    private byte[] imageShare;

    private int shareType = 1;//1网页分享 2 图片分享 3文本分享

//    public ShareObj(String shareTitle, String shareContent, String shareUrl) {
//        this.shareTitle = shareTitle;
//        this.shareContent = shareContent;
//        this.shareUrl = shareUrl;
//    }


//    public ShareObj(String shareTitle, String shareContent,
//                    String shareUrl, List<String> picUrls,
//                    String multiContent, String bizType,
//                    Long dataMerchantId, String bizData) {
//        this.shareTitle = shareTitle;
//        this.shareContent = shareContent;
//        this.shareUrl = shareUrl;
//        this.picUrls = picUrls;
//        this.multiContent = multiContent;
//        this.bizType = bizType;
//        this.dataMerchantId = dataMerchantId;
//        this.bizData = bizData;
//    }

    public ShareObj(ShareObjBuilder shareObjBuilder) {
        this.shareTitle = shareObjBuilder.shareTitle;
        this.shareContent = shareObjBuilder.shareContent;
        this.shareUrl = shareObjBuilder.shareUrl;
        this.picUrls = shareObjBuilder.picUrls;
        this.multiContent = shareObjBuilder.multiContent;
        this.bizType = shareObjBuilder.bizType;
        this.dataMerchantId = shareObjBuilder.dataMerchantId;
        this.bizData = shareObjBuilder.bizData;
        this.imageShare = shareObjBuilder.imageShare;
        this.paramsJson = shareObjBuilder.paramsJson;
        this.shareImage = shareObjBuilder.shareImage;
    }

    public static class ShareObjBuilder {
        private final String shareTitle;
        private final String shareContent;
        private final String shareUrl;
        private String shareImage;


        private byte[] bytePic;
        private List<String> picUrls;
        private JSONObject paramsJson;      //后缀参数
        private String bizType;         //分享类型
        private Long dataMerchantId;    //车辆所属商户id
        private String bizData;         //分享车辆id 或 条件
        private String multiContent;
        private byte[] imageShare; //多图分享


        public ShareObjBuilder(String shareTitle, String shareContent, String shareUrl) {
            this.shareTitle = shareTitle;
            this.shareContent = shareContent;
            this.shareUrl = shareUrl;
        }

        public ShareObjBuilder shareImage(String shareImage) {
            this.shareImage = shareImage;
            return this;
        }

        public ShareObjBuilder bytePic(byte[] bytePic) {
            this.bytePic = bytePic;
            return this;
        }

        public ShareObjBuilder picUrls(List<String> picUrls) {
            this.picUrls = picUrls;
            return this;
        }

        public ShareObjBuilder paramsJson(JSONObject paramsJson) {
            this.paramsJson = paramsJson;
            return this;
        }

        public ShareObjBuilder bizType(String bizType) {
            this.bizType = bizType;
            return this;
        }

        public ShareObjBuilder dataMerchantId(Long dataMerchantId) {
            this.dataMerchantId = dataMerchantId;
            return this;
        }

        public ShareObjBuilder bizData(String bizData) {
            this.bizData = bizData;
            return this;
        }

        public ShareObjBuilder multiContent(String multiContent) {
            this.multiContent = multiContent;
            return this;
        }

        public ShareObjBuilder imageShare(byte[] imageShare) {
            this.imageShare = imageShare;
            return this;
        }

        public ShareObj build() {
            return new ShareObj(this);
        }


    }

    public String getMultiContent() {
        return multiContent;
    }

    public void setMultiContent(String multiContent) {
        this.multiContent = multiContent;
    }

    public String getShareTitle() {
        return shareTitle;
    }

    public void setShareTitle(String shareTitle) {
        this.shareTitle = shareTitle;
    }

    public String getShareContent() {
        return shareContent;
    }

    public void setShareContent(String shareContent) {
        this.shareContent = shareContent;
    }

    public String getShareUrl() {
        return shareUrl;
    }

    public void setShareUrl(String shareUrl) {
        this.shareUrl = shareUrl;
    }

    public List<String> getPicUrls() {
        return picUrls;
    }

    public void setPicUrls(List<String> picUrls) {
        this.picUrls = picUrls;
    }

    public byte[] getBytePic() {
        return bytePic;
    }

    public void setBytePic(byte[] bytePic) {
        this.bytePic = bytePic;
    }

    public String getBizType() {
        return bizType;
    }

    public void setBizType(String bizType) {
        this.bizType = bizType;
    }

    public Long getDataMerchantId() {
        return dataMerchantId;
    }

    public void setDataMerchantId(Long dataMerchantId) {
        this.dataMerchantId = dataMerchantId;
    }

    public String getBizData() {
        return bizData;
    }

    public void setBizData(String bizData) {
        this.bizData = bizData;
    }

    public void setImageShare(byte[] imageShare) {
        this.imageShare = imageShare;
    }

    public Bitmap getBitmap() {
        if (bytePic == null) {
            return null;
        }
        return BitmapFactory.decodeByteArray(bytePic, 0, bytePic.length);//从字节数组解码位图
    }

    public void setBitmap(Bitmap bitmap) {
        bytePic = getBytes(bitmap);
    }


    public static byte[] getBytes(Bitmap bitmap) {
        //实例化字节数组输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, baos);//压缩位图
        return baos.toByteArray();//创建分配字节数组
    }

    public Bitmap getImageShare() {
        if (imageShare == null) {
            return null;
        }

        ByteArrayInputStream isBm = new ByteArrayInputStream(imageShare);//把压缩后的数据baos存放到ByteArrayInputStream中
        return BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
    }

    public void setImageShare(Bitmap imageShare) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        imageShare.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
        int options = 100;
        while (baos.toByteArray().length / 1024 > 100) {    //循环判断如果压缩后图片是否大于100kb,大于继续压缩
            baos.reset();//重置baos即清空baos
            imageShare.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%，把压缩后的数据存放到baos中
            options -= 10;//每次都减少10
        }
        this.imageShare = baos.toByteArray();//把压缩后的数据baos存放到ByteArrayInputStream中

    }

    public JSONObject getParamsJson() {
        return paramsJson;
    }

    public void setParamsJson(JSONObject paramsJson) {
        this.paramsJson = paramsJson;
    }


    public String getShareImage() {
        return shareImage;
    }

    public void setShareImage(String shareImage) {
        this.shareImage = shareImage;
    }

    public int getShareType() {
        return shareType;
    }

    public void setShareType(int shareType) {
        this.shareType = shareType;
    }
}
